package com.wfm.systemnotice.service.impl;

import com.wfm.systemnotice.entity.User;
import com.wfm.systemnotice.mapper.UserMapper;
import com.wfm.systemnotice.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author wfm
 * @since 2024-12-05
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    @Autowired
    private UserMapper userMapper;

    public List<Long> batchUpsertAndGetIds(List<User> userList) {
        // 提取所有唯一键（name）
        List<String> names = userList.stream()
                .map(User::getName)
                .collect(Collectors.toList());

        // 执行批量插入或更新
        userMapper.batchInsertOrUpdate(userList);

        // 查询所有涉及的主键 ID
        return userMapper.selectIdsByNames(names);
    }
}
